package com.tutu.order.controller;

import com.tutu.commons.utils.CollectError;
import com.tutu.commons.utils.R;
import com.tutu.commons.vo.OrdersVo;
import com.tutu.order.service.OrderService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Program: b2c-cloud-store
 * @Description:
 * @Author: JK.TuTu
 * @ @Version: 1.0
 * @Create: 2022-12-27 17:04
 **/
@Slf4j
@RestController
@RequestMapping("/order")
@ApiOperation("购物车相关Api")
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     * 生成订单接口
     *
     * @param ordersVo (用户id,购物车Vo)
     * @param result   检验结果集
     * @return 返回结果实体对象
     */
    @PostMapping("/save")
    @ApiOperation("生成订单接口")
    public R saveOrder(@RequestBody @Validated OrdersVo ordersVo, BindingResult result) {
        if (result.hasErrors()) {
            String errorInfo = CollectError.getErrorInfo(result);
            return R.fail(errorInfo);
        }
        return orderService.saveOrder(ordersVo);
    }


    /**
     * 根据用户id查看订单
     *
     * @param userId 用户id
     * @return 返回结果是实体类
     */
    @GetMapping("/list/{userId}")
    @ApiOperation("查看生成的订单")
    public R getOrders(@PathVariable("userId") Integer userId) {
        return orderService.getOrders(userId);
    }

}
